var userInfos = [];

$(document).ready(function() {
    showLoading();
    $('title', parent.document).html($('title').text());
    setBaseUrl();
	addTitle("userManageUpdate");
	createComBody("detailOneBody", userManageTable, undefined, 1, true);
    createComBody("detailTwoBody", userManageTable, undefined, 2, true);
    createComBody("detailThreeBody", userManageTable, undefined, 3, true);
	search();
});
function getRoleID(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#roleID").append("<option value='" + data[i].roleID + "'>" + data[i].roleName + "</option>");
	}
	$("#roleID").val(userInfos[0].roleID);
}
function getBranchCompany(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#branchCompanyID").append("<option value='" + data[i].branchCompanyID + "'>" + data[i].branchCompanyName + "</option>");
	}
	$("#branchCompanyID").val(userInfos[0].branchCompanyID);
}
function getChargingStation(data) {
	var length = data.length;
	for (var i = 0; i < length; i++) {
		$("#chargingStationID").append("<option value='" + data[i].chargingStationID + "'>" + data[i].chargingStationName + "</option>");
	}
	$("#chargingStationID").val(userInfos[0].chargingStationID);
}
function allClick(tabFlag){
	$(".comBody").hide();
	$("#" + tabFlag).show();
}
function search(){
	$('#UserID').val($("#detailID", parent.document).val());
	$ajax('searchUser', 'searchUserManageUpdate', searchUserManage);
	$("#detailID", parent.document).val("");
}
function searchUserManage(data){
	userInfos = data && data.userInfos || [];
	if (userInfos && userInfos.length > 0) {
		$ajaxList('role', getRoleID);
		$ajaxList('chargingStation', getChargingStation);
		$ajaxList('branchCompany', getBranchCompany);
		$("#userID").attr("readonly", true);
		$("#userID").val(userInfos[0].userID);
		$("#userName").val(userInfos[0].userName);
		$("#mailAddress").val(userInfos[0].mailAddress);
		$("#phone").val(userInfos[0].phone);
		if(userInfos[0].enabled == true){
			$("#enabled").val(1);
		}else{
			$("#enabled").val(0);
		}
//		$("#onLine").attr("readonly", true);
//		$("#loadTime").attr("readonly", true);
//		$("#onloadEquipment").attr("readonly", true);
//		$("#onLine").val(userInfos[0].onLine || "-");
//		$("#loadTime").val(userInfos[0].loadTime || "-");
//		$("#onloadEquipment").val(userInfos[0].onloadEquipment || "-");
		$('#confirm').show();
	}
	setTimeout(hideLoading, 500);
}
function confirm(){
	return checkForm("saveForm", userManageTable);
}
function saveUser(data){
	var result = data && data.result;
	if (result == "Success") {
		showMessge(messageContent["Update.Success"], undefined, goback);
	} else if (result == "NotFound") {
		showMessge(messageContent["Update.Failed"]);
	} else if (result == "BatteryNameNoNull") {
		showMessge(messageContent["Battery.BatteryNameNoNull"]);
	} else {
		showMessge(messageContent["Update.Error"]);
	}
}
function goback() {
	window.parent.skipMenu('userManage/userManage', 'drownDown5', '', 'userPage', 'userMangeLi');
}